﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;
using System.Collections;
using System.Runtime.InteropServices;

namespace BLL
{
    public class TKBKhoiBLL
    {
        QuanLyDiemDataContext DB = new QuanLyDiemDataContext();
        static int id = 0;
        //select ra cac tkb khoi dua vao nam hoc va khoi
        public IQueryable Select(string manamhoc, string makhoi)
        {
            return DB.TKBKhois.Join(
                DB.MonHocs,
                a => a.MaMonHoc,
                b => b.MaMonHoc,
                (a, b) => new
                {
                    a.MaTKBKhoi,
                    a.MaNamHoc,
                    a.MaKhoi,
                    b.TenMon
                }
                ).Join(
                DB.NamHocs,
                c => c.MaNamHoc,
                d => d.MaNamHoc,
                (c, d) => new
                {
                    c.MaTKBKhoi,
                    c.MaKhoi,
                    c.MaNamHoc,
                    c.TenMon,
                    d.TenNamHoc
                }
                ).Join(
                DB.Khois,
                e => e.MaKhoi,
                f => f.MaKhoi,
                (e, f) => new
                {
                    e.MaTKBKhoi,
                    e.MaKhoi,
                    e.MaNamHoc,
                    e.TenMon,
                    e.TenNamHoc,
                    f.TenKhoi
                }
                ).Where(s => s.MaNamHoc == manamhoc && s.MaKhoi == makhoi).Distinct().AsQueryable();
        }
        public bool Insert(TKBKhoiDTO tkb)
        {
            try
            {
                TKBKhoi tk = new TKBKhoi();
                tk.MaTKBKhoi = (id++).ToString();
                tk.MaNamHoc = tkb.MaNamHoc;
                tk.MaKhoi = tkb.MaKhoi;
                tk.MaMonHoc = tkb.MaMonHoc;
                DB.TKBKhois.InsertOnSubmit(tk);
                DB.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool Update(TKBKhoiDTO tkb)
        {
            try
            {
                TKBKhoi tk = DB.TKBKhois.Where(s => s.MaTKBKhoi == tkb.MaTKBKhoi).FirstOrDefault();
                tk.MaNamHoc = tkb.MaNamHoc;
                tk.MaKhoi = tkb.MaKhoi;
                tk.MaMonHoc = tkb.MaMonHoc;

                DB.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool Delete()
        {
            try
            {
                DB.SubmitChanges();
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

    }
}
